<template>
  <div class="merchat-list">
    <Breadcrumb>
      <BreadcrumbItem v-for="(name, i) in breadcrumb" :key="i">{{ name }}</BreadcrumbItem>
    </Breadcrumb>
    <div class="list-title">查询条件</div>
    <Row type="flex">
      <Form ref="formInline" :model="formInline" inline :label-width="0" label-position="left">
        <FormItem label="" prop="companyName">
          <Input type="text" v-model="formInline.companyName" placeholder="公司名称" clearable style="width: 150px;"/>
        </FormItem>
        <FormItem label="" prop="content">
          <Input type="text" v-model="formInline.content" placeholder="短信内容" clearable style="width: 150px;"/>
        </FormItem>
        <FormItem prop="date" label="">
          <DatePicker ref="datePicker" type="daterange" @on-change="selectDate" placeholder="请选择时间" style="width: 200px;"></DatePicker>
        </FormItem>
        <div class="form-but">
          <Button type="success" @click="getList">查询</Button>
          &nbsp;&nbsp;
          <Button @click="handleReset('formInline')">清空</Button>
        </div>
      </Form>
    </Row>
    <div>
      <Table class="list-table"
        stripe
        border
        ref="selection"
        :columns="columns"
        :data="dataList"
      ></Table>
      <div class="table-page">
        <Page :total="total" show-sizer show-elevator show-total @on-change="changePageNum" @on-page-size-change="changePageSize" />
      </div>
      <Spin size="large" fix v-if="spinShow">正在补发,不要着急,不要着急...</Spin>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { formatTime } from '@/utils'
import Api from '@/api'

const systemSmsHistoryApi = Api.SystemSmsHistory

export default {
  name: 'login',
  components: {
  },
  data() {
    return {
      breadcrumb: ['系统管理', '短信失败记录'],
      formInline: {
        companyName: '',
        content: '',
        createTimeStart: '',
        createTimeEnd: '',
      },
      dataList: [],
      columns: [
        {
          title: '主键Id',
          key: 'id',
          minWidth: 40
        },
        {
          title: '公司名称',
          key: 'companyName',
          minWidth: 80
        },
        {
          title: '接收人',
          key: 'receiver',
          minWidth: 100
        },
        {
          title: '发送状态',
          key: 'sendStatus',
          minWidth: 40
        },
        {
          title: '发送内容',
          key: 'content',
          minWidth: 180
        },
        {
          title: '创建时间',
          key: 'createTime',
          minWidth: 200,
          render: (h, params) => (
            h('div', {}, formatTime(params.row.createTime, 'second'))
          )
        },
        {
          title: '操作',
          key: 'operate',
          minWidth: 80,
          fixed: 'right',
          render: (h, params) => (
            h('div', [
              h('Button', {
                props: {
                  type: 'primary',
                  size: 'small'
                },
                style: {
                  marginRight: '15px'
                },
                on: {
                  click: () => {
                    console.log(params.row);
                    console.log(params.row.id);
                    this.sendSmsAgain(params.row.id);
                  }
                }
              }, '补发'),
            ])
          )
        }
      ],
      page: {
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      spinShow: false
    }
  },
  methods: {
    selectDate(date) {
      this.formInline.createTimeStart = date[0]
      this.formInline.createTimeEnd = date[1]
    },
    // 分页变化
    changePageNum(pageNum) {
      this.getList({ pageNum })
    },
    // 条数变化
    changePageSize(pageSize) {
      this.getList({ pageSize })
    },
    async handleReset(name) {
      this.formInline.companyName = ''
      this.formInline.createTimeStart = ''
      this.formInline.createTimeEnd = ''
      this.getList()
    },
    async getList(paramsPage = null) {
      let pageNum
      let pageSize
      let page
      const statePage = this.page
      if (paramsPage) {
        pageNum = paramsPage.pageNum || statePage.pageNum
        pageSize = paramsPage.pageSize || statePage.pageSize
        page = { pageNum, pageSize }
        this.page = { pageNum, pageSize }
      } else {
        page = statePage
      }
      const formInline = this.formInline
      const value = {}
      Object.keys(formInline).forEach(key => {
        if (formInline[key]) {
          value[key] = formInline[key]
        }
      })
      const params = {
        page,
        ...value
      }
      const { responseCode, data } = await this.$request(systemSmsHistoryApi.smsHistoryFailureList, params)
      this.dataList = [];
      this.totalCount = 0;
      if (responseCode === '1') {
        if (data.list) {
          this.dataList = data.list
          this.total = data.totalCount
        }
      }
    },
    async sendSmsAgain(id) {
      const { responseCode, responseMessage, responseType } = await this.$request(systemSmsHistoryApi.sendFailureSmsAgain, { id })
      this.$Message.success(responseMessage);
      this.sendAgainMsg = responseMessage;
    }
  },
  mounted() {
    this.getList()
  },
}
</script>
<style lang="scss" scoped>
.merchat-list {
  .ivu-form-item {
    margin-right: 20px;
  }
  .form-but {
    display: inline-block;
    margin-bottom: 24px;
  }
  .list-title {
    height: 50px;
    line-height: 50px;
    font-size: 18px;
    color: #333;
  }
  .add {
    margin-bottom: 10px;
  }
  .table-page {
    display: flex;
    justify-content: space-between;
    margin-top: 15px;
  }
  .ivu-table .table-success-row td{
    background-color: greenyellow;
    color: #fff;
  }
  .ivu-table .table-fail-row td{
    background-color: red;
    color: #fff;
  }
}
</style>
